<?php

namespace Biz\service\advertisement\apiModule;


use Biz\model\springHomeDecActivity\Wish;
use Biz\service\springHomeDecActivity\Util;
use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\service\position\PositionConstInterface;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class SingleFloatBanner extends BaseModule
{

    public function extCheckActive($v, $extInput)
    {
        if ($this->position_id == PositionConstInterface::NOTE_DETAIL_FLOAT_BANNER) {
            $noteIds = $v['single_pic_id'] ?? '';
            $noteIds = explode(',', $noteIds);
            if ($noteIds) {
                $noteId = $extInput['note_id'] ?? '';
                return in_array($noteId, $noteIds);
            }
        }
        return true;
    }

    public function format($hit, $uid, $userProfileExt, $statSignExt): array
    {
        if ($this->position_id == PositionConstInterface::HOME_FLOAT) {
            $float = null;
            $response = [
                'type' => BlockType::TYPE_HOME_FLOAT,
                'float' => &$float,
            ];

            if ($hit) {
                $float = [
                    'id' => intval($hit['id']),
                    'banner' => strlen($hit['pic_id']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '',
                    'link' => $this->generateLink($hit['link_type'], $hit['link_params']),
                    'play_times' => (int)$hit['play_times'],
                    'statSign' => $this->statSign($hit, $statSignExt),
                ];
            }

            return $response;
        }
        //家装节活动
        if ($this->position_id == PositionConstInterface::HOMEPAGE_FLOAT_BANNER) {
            if ($hit && ($hit['id'] == 60081)) {
                //家装节判断
                $hostUid = $_REQUEST['uid'] ?? 0;
                if (Wish::getInfo($hostUid)) {
                    if (HhzClient::getClientType() == HhzClient::APPTYPE_IOS) sleep(4);
                    $link = $this->generateLink(LinkConf::LINK_APP_FULLSCREEN, ['url' => Util::myFloatUrl($hostUid, false)]);
                } else {
                    $hit = [];
                }
            }
        }
        if ($this->position_id == PositionConstInterface::APP_TAB5_FLOAT_BANNER) {
            if ($hit && ($hit['id'] == 60082)) {
                //家装节判断
                if (Wish::getInfo($uid)) {
                    if (HhzClient::getClientType() == HhzClient::APPTYPE_IOS) sleep(4);
                    $link = $this->generateLink(LinkConf::LINK_APP_FULLSCREEN, ['url' => Util::myFloatUrl($uid)]);
                } else {
                    $hit = [];
                }
            }
        }
        $banner = [];
        if ($hit) {
            $pos = intval($hit['pos']);
            $banner[] = [
                'id' => intval($hit['id']),
                'index' => $pos,
                'banner' => strlen($hit['pic_id']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '',
                'link' => $link ?? $this->generateLink($hit['link_type'], $hit['link_params']),
                'statType' => 'banner' . BlockType::TYPE_CAROUSEL_BANNER,
                'statSign' => $this->statSign($hit, $statSignExt),
            ];
        }
        return [
            'type' => BlockType::TYPE_CAROUSEL_BANNER,
            'position_type' => $this->position_id,
            'banner_list' => $banner,
        ];
    }

    public function formatHits($hits)
    {
        if ($this->position_id == PositionConstInterface::HOME_FLOAT) return self::formatCurrent($hits);
        return parent::formatHits($hits); // TODO: Change the autogenerated stub
    }

}